/**
 * 
 */
package org.karen.droid.logviewtools.view.activity;

import java.util.List;

import org.karen.droid.logviewtools.R;
import org.karen.droid.logviewtools.application.LGApplication;
import org.karen.droid.logviewtools.module.LogBean;

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Karen [Email:keyu404@gmail.com]
 * 
 */
public class ShowLogActivity extends ListActivity {
    private static final String TAG = "ShowLogActivity";

    private LGApplication application = null;
    private List<LogBean> mList = null;

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_logview);

        this.application = (LGApplication) getApplication();
        initData();
        setListAdapter(new LogAdapter());
    }

    private void initData() {
        Intent i = getIntent();
        String filePath = i.getExtras().getString("file_path");
        mList = application.getLogData(filePath);
    }

    private class LogAdapter extends BaseAdapter {

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return null == mList ? 0 : mList.size();
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {
            return null;
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            return 0;
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final LogBean mLogBean = mList.get(position);
            if (null == mLogBean) {
                return null;
            }

            final ViewHolder mViewHolder;
            if (null == convertView) {
                mViewHolder = new ViewHolder();
                convertView = (LinearLayout) getLayoutInflater().inflate(R.layout.item_listview_log,
                                                                         null);
                mViewHolder.mNo = (TextView) convertView.findViewById(R.id.text_log_no_id);
                mViewHolder.mDate = (TextView) convertView.findViewById(R.id.text_log_date_id);
                mViewHolder.mLevle = (TextView) convertView.findViewById(R.id.text_log_level_id);
                mViewHolder.mTag = (TextView) convertView.findViewById(R.id.text_log_tag_id);
                mViewHolder.mMsg = (TextView) convertView.findViewById(R.id.text_log_msg_id);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (ViewHolder) convertView.getTag();
            }
            String level = mLogBean.level.trim();
            int color = -1;
            Log.d(TAG, "type is =>>" + level);
            if (level.equalsIgnoreCase("D")) {
                color = getResources().getColor(R.color.blue_d);
            } else if (level.equalsIgnoreCase("I")) {
                color = getResources().getColor(R.color.green_i);
            } else if (level.equalsIgnoreCase("W")) {
                color = getResources().getColor(R.color.yellow_w);
            } else if (level.equalsIgnoreCase("E")) {
                color = getResources().getColor(R.color.red_e);
            }

            if (-1 != color) {
                setTextColor(mViewHolder, color);
            }

            mViewHolder.mNo.setText(setNo(position));
            mViewHolder.mDate.setText(mLogBean.date);
            mViewHolder.mLevle.setText(mLogBean.level);
            mViewHolder.mTag.setText(mLogBean.tag);
            mViewHolder.mMsg.setText(mLogBean.msg);

            return convertView;
        }

        private void setTextColor(ViewHolder mViewHolder, int color) {
            mViewHolder.mDate.setTextColor(color);
            mViewHolder.mLevle.setTextColor(color);
            mViewHolder.mTag.setTextColor(color);
            mViewHolder.mMsg.setTextColor(color);
        }

        private String setNo(int position) {
            String no = "";
            if (position <= 9) {
                no = "0000" + position;
            } else if (position <= 99) {
                no = "000" + position;
            } else if (position <= 999) {
                no = "00" + position;
            } else if (position <= 9999) {
                no = "0" + position;
            } else if (position <= 99999) {
                no = "" + position;
            } else {
                no = no + position;
            }
            return no;
        }

        private class ViewHolder {
            public TextView mNo;
            public TextView mDate;
            public TextView mLevle;
            public TextView mTag;
            public TextView mMsg;
        }

    }

}
